package com.tanhua.server.controller;

import com.tanhua.domain.db.Settings;
import com.tanhua.domain.db.User;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/users")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;
    /**
     * 查询用户信息
     * GET /users
     */
    @GetMapping
    public ResponseEntity findUserInfo(@RequestParam(required = false)Long userId, @RequestParam(required = false)Long huanxiId){
    User user= UserHolder.getUser();
        Long id = user.getId();
        if (userId!=null){
            id=userId;
        }else if(huanxiId!=null){
            id=huanxiId;
        }
       return  userInfoService.findUserInfo(id);
    }
    /**
     * 我的-编辑-更新用户信息
     * PUT /users
     */
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo){
        return userInfoService.updateUserInfo(userInfo);
    }
    /**
     * 我的-通用设置-查询通用设置
     * GET /users/settings
     */

    @GetMapping("/settings")
    public ResponseEntity findSettings(){
        return userInfoService.findSettings();
    }
/**
 * 我的-通用设置-黑名单列表
 * GET/users/blacklist
 */
  @GetMapping("/blacklist")
    public ResponseEntity findBlackList(@RequestParam(defaultValue = "1")int page,
                                       @RequestParam(defaultValue = "10")int pageSize){
        return userInfoService.findBlackList(page,pageSize);

  }
    /**
     * 我的-通用设置-黑名单-移除黑名单
     * DELETE /users/blacklist/:uid
     */
    @DeleteMapping("//blacklist/{blackUserId}")
    public  ResponseEntity removeBlackList(@PathVariable("blackUserId") Long blackUserId){
      return   userInfoService.removeBlackList( blackUserId);
    }
    /**
     * 我的-通用设置-陌生人问题-修改陌生人问题
     * POST /users/questions
     */
    @PostMapping("/questions")
    public ResponseEntity updateQuestion(@RequestBody Map<String,String> map){
        String content = map.get("content");
        return  userInfoService.updateQuestion(content);
    }
    /**
     * 我的-通用设置-通知设置-保存通知设置
     * POST/users/notifications/setting
     */
    @PostMapping("/notifications/setting")
    public ResponseEntity  saveSettings(@RequestBody Settings settings){
        return userInfoService.saveSettings(settings);
    }
    /**
 * 我的-通用设置-修改手机号-发送短信验证码
 * POST /users/phone/sendVerificationCode
 */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity  sendVerificationCode(){
        return userInfoService.sendVerificationCode();
    }
    /**
     * 我的-通用设置-修改手机号-校验验证码
     * POST/users/phone/checkVerificationCode
     */
    @PostMapping("/phone/checkVerificationCode")
    public  ResponseEntity checkVerificationCode(@RequestBody Map map){
        String verificationCode = (String) map.get("verificationCode");
        return userInfoService.checkVerificationCode(verificationCode);
    }
    /**
     * 我的-通用设置-修改手机号-保存
     * POST/users/phone
     */
    @PostMapping("/phone")
    public ResponseEntity  updateMobile (@RequestBody Map map){
        String newPhone = (String) map.get("phone");
        return  userInfoService.updateMobile(newPhone);
    }
    /* 我的-喜欢 粉丝 相互喜欢的数量
    * Get /users/count*/
    @GetMapping("/counts")
    public ResponseEntity userLikeCounts(){
        return userInfoService.userLikeCounts();
    }
    /**
     * 我的-喜欢、粉丝、相互喜欢 分页列表
     * GET /users/friends/:type
     * @param type 类型 1互相关注(相互喜欢)  2我关注(我喜欢的)  3粉丝(喜欢我的)  4谁看过我
     * @param page 页码
     * @param pagesize 查询几条
     */
    @GetMapping("/friends/{type}")
    public ResponseEntity userLikeList(@PathVariable("type")Integer type,
                                       @RequestParam(defaultValue = "1") int page,
                                       @RequestParam(defaultValue = "10")int pagesize){
        return userInfoService.userLikeList(type,page,pagesize);
    }
    /**
     * 我的-粉丝-喜欢
     * POST /users/fans/:uid
     */
    @PostMapping("/fans/{fansId}")
    public ResponseEntity likeFans(@PathVariable("fansId") Long fansId){
        return userInfoService.likeFans(fansId);
    }

}
